/*********************************************************************\
*                                                                     *
*         Jamper - Java XML mapper (visual XSLT generator)            *
*                Copyright (C) 2005 Filip Pavlovic                    *
*                    sqba@users.sourceforge.net                       *
*                                                                     *
*  This program is free software; you can redistribute it and/or      *
*  modify it under the terms of the GNU General Public License        *
*  as published by the Free Software Foundation; either version 2     *
*  of the License, or (at your option) any later version.             *
*                                                                     *
*  This program is distributed in the hope that it will be useful,    *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of     *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *
*  GNU General Public License for more details.                       *
*                                                                     *
*  You should have received a copy of the GNU General Public License  *
*  along with this program; if not, write to the Free Software        *
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston,             *
*  MA 02111-1307, USA.                                                *
*                                                                     *
\*********************************************************************/


package sqba.jamper;


//import sqba.jamper.tree.MapperTreeNode;
import sqba.jamper.map.object.IMapObject;
import sqba.jamper.map.tree.MapperTree;
import sqba.jamper.util.XMLMapperInit;
//import sqba.jamper.util.XMLUtils;

import javax.swing.JMenuItem;
import javax.swing.JSplitPane;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

//import java.io.File;
//import java.io.IOException;
//import java.io.DataOutputStream;
//import java.io.FileOutputStream;
//import java.io.BufferedOutputStream;
//import java.io.FileNotFoundException;

import javax.swing.JMenu;
import javax.swing.AbstractAction;

//import java.net.URL;
//import java.net.URISyntaxException;


/**
 * MapView - main view that consists of left and
 * right tree, map (line) view and property view.
 *
 * @author	Filip Pavlovic
 * @version	1.0
 */
public class MapView extends JSplitPane
{
    private JSplitPane		_leftPane, _rightPane;
    public PropertyPane 		_propView;

    public ObjectPanel		_objectPanel;
    public MapperTree		_srcTree, _dstTree;
    public JamperFrame		_mapper;


    class MyFileMenu extends JMenu
    {
		public MyFileMenu(String name)
    	{
    		super(name);

			JMenuItem menuItem;

			menuItem = new JMenuItem("Load...");
	        menuItem.addActionListener(new AbstractAction() {
		        public void actionPerformed(ActionEvent e) {
		        	MapView.this.loadMap(XMLMapperInit.browse(true));//, false);
		        }
		    });
			this.add(menuItem);

			menuItem = new JMenuItem("Save...");
	        menuItem.addActionListener(new AbstractAction() {
		        public void actionPerformed(ActionEvent e) {
		        	MapView.this.saveMap(XMLMapperInit.browse(false));
		        }
		    });
			this.add(menuItem);
		}
	}

    class MyMenu extends JMenu
    {
		public MyMenu()
    	{
    		super("File");
			this.add(new MyFileMenu("Map"));
			this.add(MapView.this._srcTree.getMenu());
			//this.add(SplitFrame.this._objectPanel.getFileMenu());
			this.add(MapView.this._dstTree.getMenu());

			this.addSeparator();

			JMenuItem menuItem;

			/*menuItem = new JMenuItem("Load Example...");
	        menuItem.addActionListener(new AbstractAction() {
		        public void actionPerformed(ActionEvent e) {
					Class c = sqba.jamper.util.XMLMapperInit.class;
					java.net.URL url = c.getResource("/sqba/example/map.xml");
					try {
						System.out.println("test");
						System.out.println(url.getFile());
						System.out.println(url.getPath());
						System.out.println(url.toURI());
		        		MapView.this.loadMap(url.getPath());//, true);
		        	} catch(URISyntaxException ue) {
		        		System.out.println("URISyntaxException: " + ue);
		        	}
		        }
		    });
			this.add(menuItem);

			this.addSeparator();*/
	
			menuItem = new JMenuItem("Exit");
	    	menuItem.addActionListener(new AbstractAction() {
		        public void actionPerformed(ActionEvent e) {
		        	System.exit(0);
		        }
		    });
			this.add(menuItem);
		}
	}

	public JMenu getMenu()		{ return new MyMenu(); }
	//public JMenu getFileMenu()	{ return new MyFileMenu("Map"); }

    public MapView(JamperFrame mapper)
    {
    	super(JSplitPane.VERTICAL_SPLIT, true);

    	_mapper = mapper;

	    /////////////////////////////////////////////////////////////
	    // Creating main components
	    /////////////////////////////////////////////////////////////
		_srcTree = new MapperTree(false);
		_dstTree = new MapperTree(true);

		//_srcTree.setShowValues(false);
		//_dstTree.setShowValues(true);

		_objectPanel	= new ObjectPanel(this, _srcTree, _dstTree);
		_propView	= new PropertyPane(this);

		_leftPane	= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
		_rightPane	= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);

		_srcTree.addTreeSelectionListener(_propView);
		_dstTree.addTreeSelectionListener(_propView);

		//_leftPane.setContinuousLayout(true);
		//_rightPane.setContinuousLayout(true);
		//this.setContinuousLayout(true);

		_leftPane.setBorder(null);
		_rightPane.setBorder(null);
		this.setBorder(null);

		/////////////////////////////////////////////////////////////
		// Positioning the main components between the splitters
		/////////////////////////////////////////////////////////////
		_leftPane.setLeftComponent(_srcTree.getScrollPane());
		_leftPane.setRightComponent(_rightPane);

		_rightPane.setLeftComponent(_objectPanel);
		_rightPane.setRightComponent(_dstTree.getScrollPane());

		this.setTopComponent(_leftPane);
		this.setBottomComponent(_propView);

		_leftPane.setDividerSize(2);
		_rightPane.setDividerSize(2);

		addComponentListener(new ComponentListener() {
			public void componentHidden(ComponentEvent e) {}
			public void componentMoved(ComponentEvent e) {}
			public void componentResized(ComponentEvent e)
			{
				Dimension d = e.getComponent().getSize();
				setSize(d);

				int x = getWidth()/3;
				_rightPane.setDividerLocation(x);
				_leftPane.setDividerLocation(x);
				MapView.this.setDividerLocation((int)((getHeight()/3)*2));
			}
			public void componentShown(ComponentEvent e) {}
		});
	}

	public void render() {
		_objectPanel.render();
	}

	/*public void loadSource(String uri)
	{
		_srcTree.load(uri);
	}
	
	public void setSrcRoot(String path)
	{
		_srcTree.setRoot(path);
	}
	
	public void loadDestination(String uri)
	{
		_dstTree.load(uri);
	}
	
	public void setDstRoot(String path)
	{
		_dstTree.setRoot(path);
	}*/

	/*public void loadMapObjects(String uri)
	{
		_objectPanel.load(uri);
	}*/

	public void loadMap(String uri)//, boolean bResource)
	{
		//MapLoader ml = new MapLoader(this);
		//MapLoader ml = new MapLoader(_objectPanel.getMap());
		//ml.load(uri);//, false);
		_objectPanel.getMap().load(uri);
	}
	
	public void saveMap(String uri)
	{
		//MapLoader ml = new MapLoader(this);
		//MapLoader ml = new MapLoader(_objectPanel.getMap());
		//ml.save(uri);
		_objectPanel.getMap().save(uri);
	}
	
	public void objectSelected(IMapObject sel)
	{
		if(null != _mapper)
			_mapper.objectSelected(sel);
	}
	
	public void setXSLT(String xslt)
	{
		if(null != _mapper)
			_mapper.setXSLT(xslt);
	}
}
